package leetcode.剑指offer.剑指II;

/**
 * @author ：zsy
 * @date ：Created 2021/10/31 11:01
 * @description：
 */
import java.util.*;

public class Main {
    public static void main(String[]args) {
        int[] nums = new int[]{2, 7, 4, 1, 8};
        System.out.println(lastStoneWeight(nums));
    }

    public static int lastStoneWeight(int[] stones) {
        PriorityQueue<Integer> pq = new PriorityQueue<Integer>((a, b) -> b - a);
        for (int stone : stones) {
            pq.offer(stone);
        }

        while (pq.size() > 1) {
            int a = pq.poll();
            int b = pq.poll();
            if (a > b) {
                pq.offer(a - b);
            }
        }
        return pq.isEmpty() ? 0 : pq.poll();
    }
}
